import os
from dotenv import load_dotenv
from typing import Optional

# 加载环境变量
load_dotenv()


class Settings:
    """应用配置类"""
    
    # AI服务配置 (Deepseek-V3)
    DEEPSEEK_API_KEY: Optional[str] = os.getenv("DEEPSEEK_API_KEY")
    DEEPSEEK_BASE_URL: str = os.getenv("DEEPSEEK_BASE_URL", "https://api.siliconflow.cn/v1")
    DEEPSEEK_MODEL: str = os.getenv("DEEPSEEK_MODEL", "deepseek-ai/DeepSeek-V3")
    DEEPSEEK_MAX_TOKENS: int = int(os.getenv("DEEPSEEK_MAX_TOKENS", "30000"))
    DEEPSEEK_TEMPERATURE: float = float(os.getenv("DEEPSEEK_TEMPERATURE", "0"))
    
    # 应用配置
    APP_ENV: str = os.getenv("APP_ENV", "development")
    DEBUG: bool = os.getenv("DEBUG", "True").lower() == "true"
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    
    # 服务端口配置
    STREAMLIT_PORT: int = int(os.getenv("STREAMLIT_PORT", "8501"))
    API_PORT: int = int(os.getenv("API_PORT", "8500"))
    API_HOST: str = os.getenv("API_HOST", "0.0.0.0")
    
    # API配置
    API_PREFIX: str = os.getenv("API_PREFIX", "/api/v1")
    API_TITLE: str = os.getenv("API_TITLE", "牛马工时日志汇报系统API")
    API_VERSION: str = os.getenv("API_VERSION", "1.0.0")
    API_DESCRIPTION: str = os.getenv("API_DESCRIPTION", "AI驱动的工时日志自动总结汇报API")
    
    # 数据库配置
    DB_HOST: str = os.getenv("DB_HOST", "192.168.1.25")
    DB_PORT: int = int(os.getenv("DB_PORT", "3306"))
    DB_USER: str = os.getenv("DB_USER", "agi")
    DB_PASSWORD: str = os.getenv("DB_PASSWORD")
    DB_NAME: str = os.getenv("DB_NAME", "workhourmanagementdb_v2")
    
    def __init__(self):
        """初始化配置，并验证必要的配置项"""
        if not self.DEEPSEEK_API_KEY and self.APP_ENV == "production":
            print("⚠️ 警告: DEEPSEEK_API_KEY 未设置，AI 功能将不可用")
    
    @property
    def is_development(self) -> bool:
        """是否为开发环境"""
        return self.APP_ENV == "development"
    
    @property
    def is_production(self) -> bool:
        """是否为生产环境"""
        return self.APP_ENV == "production"
    
    @property
    def ai_enabled(self) -> bool:
        """AI服务是否可用"""
        return bool(self.DEEPSEEK_API_KEY)
    
    @property
    def ai_model(self) -> str:
        """获取AI模型名称"""
        return self.DEEPSEEK_MODEL
    
    def get_api_url(self) -> str:
        """获取API完整URL"""
        return f"http://{self.API_HOST}:{self.API_PORT}"
    
    def get_streamlit_url(self) -> str:
        """获取Streamlit完整URL"""
        return f"http://{self.API_HOST}:{self.STREAMLIT_PORT}"


# 全局配置实例
settings = Settings() 